package com.oceansoft.core.message;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class Message {


    
    public static String BASE_URL = "http://10.35.252.76:8080/imweb";

 	public static String URL_SUFFIX =  ".json";

 	public static String APPID =  "system";//"system";

 	public static String APPKEY = "123456789";// "123456789";
 	
 	public static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat(
			"yyyyMMddHHmmss");
     
 	/**
 	 * 
 	 * @param title
 	 * @param receiver
 	 * @param content
 	 */
     public static void pushMessage(String receiver,String title,String content)
     {
    	String api = "/api/message/set";
         
 		String url = BASE_URL + api + URL_SUFFIX;

 		// 公共
 		HashMap<String, String> params = setPublic();

 		// 私有必填

 		params.put("guid", UUID.randomUUID().toString());
 		params.put("body", content);
 		params.put("fromname", "系统推送");
 		params.put("to", receiver);
 		params.put("orgid", "default");

 		String time = TIME_FORMAT.format(new Date());

 		params.put("starttime", time);

 		// 私有可选
 		params.put("url", "http://10.35.252.116:8080/gabzjl/cas/login.jsp");
 		params.put("title", title);
 		params.put("category", "奖励工作管理平台");
 		params.put("msglevel", "2");

 		String token = null;

 		try {
 			token = TokenUtil.makeToken(api, params, APPKEY);
 		} catch (ApiException e1) {
 			e1.printStackTrace();
 		}

 		params.put("token", token);

 		try {
 			String str = HttpClientUtil.postRequest(url, params, null, "http");
 			int res = str.indexOf("\"code\":0") == -1 ? 0 : 1;
 			if(0==res)
 			{
 				System.out.println(str);
 			}
 			System.out.println(str);
 			//return res;

 		} catch (ApiException e) {
 			e.printStackTrace();
 			//return 0;
 		}
     }
     private static HashMap<String, String> setPublic() {

 		String[] open = {
 				"b07606506b2d4bd1d6a03623e029442b11ce75b06f6baead",
 				"1ca603737ea35372796199a55bc0663fa2d18375dd38d002f564e61ff6b193183d3500b3061f4fa8580a09fdfab73018" };
 		HashMap<String, String> params = new HashMap<String, String>();
 		params.put("openid", open[0]);
 		params.put("openkey", open[1]);

 		params.put("appid", APPID);

 		return params;
 	}
}
